//
//  PCDDataBaseModel.m
//  PCDBank
//
//  Created by lyy on 2021/3/17.
//  Copyright © 2021 DK. All rights reserved.
//

#import "PCDDataBaseModel.h"

#define PCDDataBase @"PDMPData.db"

#define PCDDataBaseSyn @"PCDDataSYN.db"

@implementation PCDDataBaseModel

+ (instancetype)shareModel {
    static PCDDataBaseModel *model = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        model = [[self alloc] init];
    });
    return model;
}

- (NSMutableDictionary *)memoryStorage {
    if (!_memoryStorage) {
        _memoryStorage = [NSMutableDictionary dictionary];
    }
    return _memoryStorage;
}

- (NSDictionary *)dictionaryForJsonData:(NSData *)jsonData {
    if (![jsonData isKindOfClass:[NSData class]] || jsonData.length < 1) {
        return nil;
    }
    id jsonObj = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
    if (![jsonObj isKindOfClass:[NSDictionary class]]) {
        return nil;
    }
    return [NSDictionary dictionaryWithDictionary:(NSDictionary *)jsonObj];
}

- (NSData *)compactFormatDataForDictionary:(NSDictionary *)dicJson {
    
    if (![dicJson isKindOfClass:[NSDictionary class]]) {
        return nil;
    }
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dicJson options:0 error:nil];
    if (![jsonData isKindOfClass:[NSData class]]) {
        return nil;
    }
    return jsonData;
}


- (NSString *)getStorageInfoFromDataBase {
    NSData *storageData = [PCDDataStorageMangerGet() cacheForFileWithFileName:kPCDDataBase];
    NSDictionary *dict = [self dictionaryForJsonData:storageData];
    NSDictionary *callbackDict = @{@"keys":(dict ? dict.allKeys:@[]),@"currentSize":(storageData?@(storageData.length):@(0)),@"limitSize":@"10240"};
    return [callbackDict JSONRepresentation];
}


- (NSString *)setStorageToDataBaseWith:(NSDictionary *)data {
    
    NSData *storageData = [PCDDataStorageMangerGet() cacheForFileWithFileName:kPCDDataBase];
    NSDictionary *dict = [self dictionaryForJsonData:storageData];
    
    NSMutableDictionary *mDict = [NSMutableDictionary dictionaryWithDictionary:dict];
    
    NSString *key = [data objectForKey:@"key"];
    id dataInfo = [data objectForKey:@"data"];
    NSString *dataString = @"";
    if ( [dataInfo isKindOfClass:[NSString class]]) {
        PCDLog(@"字符串");
        dataString = dataInfo;
    } else if ( [dataInfo isKindOfClass:[NSDictionary class]]||[dataInfo isKindOfClass:[NSArray class]]) {
        PCDLog(@"字典 数组");
        dataString = [dataInfo JSONRepresentation];
    }  else if ( [dataInfo isKindOfClass:[NSData class]]) {
        PCDLog(@"NSData");
        dataString = [[NSString alloc] initWithData:dataInfo encoding:NSUTF8StringEncoding];
    }
    [mDict setValue:dataString forKey:key];
    
    NSData *saveData = [self compactFormatDataForDictionary:[NSDictionary dictionaryWithDictionary:mDict]];
    [PCDDataStorageMangerGet() setCacheForFile:saveData FileName:kPCDDataBase];
    
    return @"";
}

- (NSString *)getStorageFromDataBaseWith:(NSDictionary *)data {
    NSData *storageData = [PCDDataStorageMangerGet() cacheForFileWithFileName:kPCDDataBase];
    NSDictionary *dict = [self dictionaryForJsonData:storageData];
    NSString *key = [data objectForKey:@"key"];
    return [PCDUtil safeString:[dict objectForKey:key]];
}

- (NSString *)removeStorageFromDataBaseWith:(NSDictionary *)data {
    if (!data) {
        return @"";
    }
    if (![data isKindOfClass:NSDictionary.class]) {
        return @"";
    }
    NSData *storageData = [PCDDataStorageMangerGet() cacheForFileWithFileName:kPCDDataBase];
    NSDictionary *dict = [self dictionaryForJsonData:storageData];
    
    NSMutableDictionary *mDict = [NSMutableDictionary dictionaryWithDictionary:dict];
    NSString *key = [data objectForKey:@"key"];
    [mDict removeObjectForKey:key];
    
    NSData *saveData = [self compactFormatDataForDictionary:[NSDictionary dictionaryWithDictionary:mDict]];
    [PCDDataStorageMangerGet() setCacheForFile:saveData FileName:kPCDDataBase];
    
    return @"";
    
}

- (NSString *)clearStorageFromDataBase {
    
    NSDictionary *dict = @{};
    NSData *saveData = [self compactFormatDataForDictionary:dict];
    [PCDDataStorageMangerGet() setCacheForFile:saveData FileName:kPCDDataBase];
    return @"";
}

@end
